package main

import (
	"encoding/json"
	"fmt"
)

type Addr struct {
	Province string
	City     string
}
type Stul struct {
	Name string `json:"name"` // 结构体的标签
	Age  int    `json:"-"`    // 表示不参与序列化
	Addr Addr
}

func main() {

	var stuMap = map[string]interface{}{"name": "yuan", "age": 32, "addr": "beijing"}
	var stuStruct = Stul{Name: "yuan", Age: 18, Addr: Addr{Province: "Hebei", City: "langFang"}}

	// 序列化
	jsonStuMap, _ := json.Marshal(stuMap)
	jsonStuStruct, _ := json.Marshal(stuStruct)

	fmt.Println(string(jsonStuMap))
	fmt.Println(string(jsonStuStruct))

	// 反序列化
	// var x  = make(map[int]string)
	var StuMap map[string]interface{}
	err := json.Unmarshal(jsonStuMap, &StuMap)
	if err != nil {
		return
	}
	fmt.Println("StuMap", StuMap, StuMap["name"])

	var StuStruct Stul
	errl := json.Unmarshal(jsonStuStruct, &StuStruct)
	if errl != nil {
		return
	}
	fmt.Println(StuStruct)
	fmt.Println(StuStruct.Name)
	fmt.Println(StuStruct.Addr.City)

}
